package iscript.gameFramework.GameLite.content
{
	import com.greensock.loading.data.DataLoaderVars;
	import com.greensock.loading.data.ImageLoaderVars;
	import com.greensock.loading.data.SWFLoaderVars;
	import com.greensock.loading.data.XMLLoaderVars;
	import com.greensock.loading.DataLoader;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderStatus;
	import com.greensock.loading.SWFLoader;
	import com.greensock.loading.XMLLoader;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.text.Font;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	import iscript.gameFramework.GameLite.core.GameComponent;
	import iscript.gameFramework.GameLite.core.GameController;
	import iscript.gameFramework.GameLite.core.GameModel;
	import iscript.gameFramework.GameLite.events.GameContentEvent;
	import iscript.gameFramework.GameLite.Game;
	
	 
	
	[Event(name="everything",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	[Event(name="onloadImage",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	[Event(name="onloadsound",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	[Event(name="onloadxml",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	[Event(name="addBitmapData",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	[Event(name="addComponent",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	[Event(name="addComponent",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	[Event(name="addModel",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	[Event(name="addController",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	[Event(name="addxmldata",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	[Event(name="addObjectContent",type="iscript.gameFramework.GameLite.events.GameContentEvent")]
	
	
	public class GameContent extends EventDispatcher
	{
		
		public function GameContent()
		{
			_keyContent = new Dictionary();
			_swfContent = new SwfContent(this);
			_imageContent = new ImageContent(this);
			_xmloaderContent = new XMLLoaderContent(this);
			_dataLoaderContent = new DataLoaderContent(this);
			
			_bitmapdatagroupContent = new BitmapDataGroupContent(this);
			_componentContent = new ComponentContent(this);			
			_objectContent = new ObjectContent(this);
			_watchGameContentEventList = new WatchGameContentEventList();						  
			_xmldataContent = new XMLDataContent(this);
			_modelContent = new GameModelContent(this);
			_controllerContent = new GameControllerContent(this);
			
		}
		
		static  internal  const GamneContentKey:String = "GamneContentKey";
		
		private var _keyContent:Dictionary;
		private var _swfContent:SwfContent;
		private var _imageContent:ImageContent;
		private var _bitmapdatagroupContent:BitmapDataGroupContent;
		private var _componentContent:ComponentContent;
		private var _objectContent:ObjectContent;
		private var _xmldataContent:XMLDataContent;
		private var _watchGameContentEventList:WatchGameContentEventList;
		private var _modelContent:GameModelContent;
		private var _xmloaderContent:XMLLoaderContent;
		private var _dataLoaderContent:DataLoaderContent;
		private var _controllerContent:GameControllerContent;
		
		
		
		/*====EVENT==============================================================================*/
		/**
		 * [GameContentEvent]监听资源在GameContent的情况  当资源完整体进入GameContent的时候抛出 自动移除监听事件只监听一次
		 * @param	contentkey
		 * @param	callback
		 */
		public function addOnceGameContentListener(contentkey:String, callback:Function):void {
			      _addGameContentListenerLogic(contentkey, callback, true);
			    }
				
		
		/**
		 * [Event:GameContentEvent]监听资源在GameContent的情况 当资源完整体进入GameContent的时候抛出
		 * @param	contentkey
		 * @param	callback
		 */
		public function addGameContentListener(contentkey:String, callback:Function):void {
				  _addGameContentListenerLogic(contentkey, callback, false);
				}
			 		
		 
		private function _addGameContentListenerLogic(contentkey:String, callback:Function,auotRemoveEvent:Boolean):void {
			      var processFn:Function = function(e:GameContentEvent):void {					                                     
					                                  switch(contentkey) {
														    case GameContentEvent.ADDBITMAPDATA:
																  callback(e);
																   if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
														    case GameContentEvent.ADDCOMPONENT:
															      callback(e);
																   if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
														    case GameContentEvent.ADDOBJECTCONTENT:
															callback(e);
															 if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
														    case GameContentEvent.ADDXMLDATA:
															callback(e);
															 if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
														    case GameContentEvent.EVERYTHING:
															callback(e);
															 if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
														    case GameContentEvent.ONLOADIMAGE:
															callback(e);
															 if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
														    case GameContentEvent.ONLOADSOUND:
															callback(e);
															 if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
														    case GameContentEvent.ONLOADSWF:
															callback(e);
															 if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
														    case GameContentEvent.ONLOADXML:
															callback(e);
															 if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
															case GameContentEvent.ADDMODEL:
															callback(e);
															 if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
															case GameContentEvent.ADDCONTROLLER:
															callback(e);
															 if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
															break;
														    default:
															if (contentkey == GameContentEvent(e).key) {														 
														        callback(e)
														        if(auotRemoveEvent){removeGameContentListener(contentkey,callback)}
														    }															
														  } 													 
												    };			     
			      _watchGameContentEventList.add(contentkey,callback,processFn);	       
			      this.addEventListener(GameContentEvent.EVERYTHING, processFn);
				  
				 var content:*=null;
				 switch(_keyContent[contentkey]) {
					    case SwfContent:
							var swfloader:SWFLoader = getSWF(contentkey);
							if (swfloader.status == LoaderStatus.COMPLETED)
							content  =swfloader ;
						break; 
					case ImageContent:
						var imageloader:ImageLoader = getImage(contentkey);
						if (imageloader.status == LoaderStatus.COMPLETED)
						content =  imageloader;
						break;  
					case XMLLoaderContent:
						var xmlloader:XMLLoader = getXMLoader(contentkey);
						if (xmlloader.status == LoaderStatus.COMPLETED)
						content =  xmlloader;
						break;  
					case DataLoaderContent:
						var dataloader:DataLoader = getDataLoader(contentkey);
						if (dataloader.status == LoaderStatus.COMPLETED)
						content =  dataloader;
						break;
					case XMLDataContent:
						content =  getXMLData(contentkey);
						break;
					case BitmapDataGroupContent:
						content =  getBitmapDataGroup(contentkey);
						break;
					 case ComponentContent:
						content =  getComponent(contentkey);
						break; 
					case GameModelContent:
						content =  getModel(contentkey);
						break; 
					case GameControllerContent:
						content =  getController(contentkey);
						break; 
					case ObjectContent:
						content =  getObject(contentkey);
						break;
					 }
				  if (content!=null) {
					    var e:GameContentEvent = new GameContentEvent(GameContentEvent.EVERYTHING)
						    e.key = contentkey;
							e.content = content;
							this.dispatchEvent(e);
					  }
		}	
			
		public function removeGameContentListener(contentkey:String, callback:Function):void {
			    var list:Vector.<WatchGameContentItem> = _watchGameContentEventList.getWatchGameContentItem(contentkey,callback);
				for each( var item:WatchGameContentItem  in list) {
					 this.removeEventListener(GameContentEvent.EVERYTHING,item.processFn);		
				}
				_watchGameContentEventList.remove(contentkey, callback);
		}
		
		/*====BitmapDataGroup==============================================================================*/
		public function addBitmapDataGroup(key:String, data:Vector.<BitmapData>):void
		{
			if (_keyContent[key]==null) {
				 _keyContent[key] = BitmapDataGroupContent;
			      _bitmapdatagroupContent.add(key, data);
			}
		}
		
		public function getBitmapDataGroup(key:String):Vector.<BitmapData>
		{
			return _bitmapdatagroupContent.get(key);
		}
		
		
		public function containBitmapDataGroup(key:String):Boolean {return _bitmapdatagroupContent.contain(key);}
		
		/*====Image==============================================================================*/
		public function addImage(url:String, key:String=null,vars:ImageLoaderVars = null):void
		{
			if (key == null) key = url;
			//if(_keyContent.add(key,ImageContent)){
			if (_keyContent[key] == null) {
				_keyContent[key] = ImageContent;
				_imageContent.add(url,key, vars);
			}
		}
		
		public function getImage(key:String):ImageLoader
		{
			return _imageContent.get(key);
		}
		
		public function containImage(key:String):Boolean { return _imageContent.contain(key); }
		
		
		internal function addImageLoaderObject(key:String, imageload:ImageLoader):void {
			   if (_keyContent[key] == null) {
				   _keyContent[key] = ImageContent;
			       _imageContent.addImageLoader(key,imageload);
			  }
			}
		
		/*====SWF==============================================================================*/
		public function addSwf(url:String, key:String=null,vars:SWFLoaderVars = null):void
		{
			if (key == null) key = url;
			if (_keyContent[key] == null) {
			    _keyContent[key] = SwfContent;
				_swfContent.add(url,key, vars);
			}
			
		}
		
		public function getSWF(key:String):SWFLoader
		{
			return _swfContent.get(key);
		}
		public function containSWF(key:String):Boolean { return _swfContent.contain(key); } 
		
		internal function addSWFLoaderObject(key:String, swfload:SWFLoader):void {
			  //if(_keyContent.add(key,SwfContent)){
			if (_keyContent[key] == null) {
				_keyContent[key] = SwfContent;
			    _swfContent.addSWFLoader(key,swfload);
			  }
			}
		
		
		/*====Component==============================================================================*/
		public function addComponent(comp:GameComponent):void {
			   var key:String =  getQualifiedClassName(comp).split( "::" ).join( "." );
			   if (_keyContent[key] == null) {
				   _keyContent[key] = ComponentContent;
			       _componentContent.add(comp);
			   }
			}

		public function containComponent(classORpackname:*):Boolean {  
			   var key:String;
			   if (classORpackname is Class) { key =  getQualifiedClassName(classORpackname).split( "::" ).join( "." ); }
			   key = classORpackname;
			   return   _componentContent.contain(key);
			}
			
		public function getComponent(classORpackname:*):GameComponent {  
			   var key:String;
			   if (classORpackname is Class) { key = getQualifiedClassName(classORpackname).split( "::" ).join( "." ); }
			   key = classORpackname;
			   return   _componentContent.get(key);
			}

		/*====ObjectContent==============================================================================*/
		public function addObject(key:String, object:Object):void { 
			   if (_keyContent[key] == null) {
				  _keyContent[key] = Object;
			      _objectContent.add(key, object);
			   }
			}
			
		public function containObject(key:String):Boolean {
			  return _objectContent.contain(key);
			}
			
		public function getObject(key:String):Object{  
			    return _objectContent.get(key);
			}	
			
	    /*====XMLdataContent==============================================================================*/
		public function addXMLData(key:String, data:XML):void { 
			   if ( _keyContent[key] == null) {
				     _keyContent[key] = XML;  
			         _xmldataContent.add(key, data);
			   }
			}
			
		public function containXMLData(key:String):Boolean {
			  return _xmldataContent.contain(key);
			}
			
		public function getXMLData(key:String):XML{  
			    return _xmldataContent.get(key);
			}	
			
		/*====XMLoaderContent==============================================================================*/
		public function addXMLoader(url:String, key:String=null,vars:XMLLoaderVars = null):void { 
			   if (key == null) key = url;
			   //if(_keyContent.add(key,XMLLoaderContent)){
		        if (_keyContent[key] == null) {
					_keyContent[key]  = XMLLoaderContent;
			       _xmloaderContent.add(url,key, vars);
				}
			}
			
		public function containXMLoader(key:String):Boolean {
			  return _xmloaderContent.contain(key);
			}
			
		public function getXMLoader(key:String):XMLLoader{  
			  return _xmloaderContent.get(key);
			}	
			
	  	 internal function addXMLLoaderObject(key:String, xmload:XMLLoader):void {
		  //if(_keyContent.add(key,XMLLoaderContent)){
			if (_keyContent[key] == null) {
				_keyContent[key] = XMLLoaderContent;
		      _xmloaderContent.addXMLoader(key,xmload);
		    }
		}	
					
		/*====DataLoaderContent==============================================================================*/
		
		public function addDataLoader(urlOrRequest:*, key:String=null,vars:DataLoaderVars = null):void { 
			   if (key == null) 
			   if (urlOrRequest is URLRequest) { 
				   key = URLRequest(urlOrRequest).url; }
				   else {
				   key = urlOrRequest;
				   }
				//if(_keyContent.add(key,DataLoaderContent)){
				if (_keyContent[key] == null) {
					_keyContent[key] = DataLoaderContent;
				_dataLoaderContent.add(urlOrRequest,key, vars);
				}
			}
			
		public function containDataLoader(key:String):Boolean {
			  return _dataLoaderContent.contain(key);
			}
			
		public function getDataLoader(key:String):DataLoader{  
			  return _dataLoaderContent.get(key);
			}	
			
	 	internal function addDataLoaderObject(key:String, dataload:DataLoader):void {
			 if (_keyContent[key] == null) {
				 _keyContent[key]  = DataLoaderContent;
				_dataLoaderContent.addDataLoader(key, dataload);
		     }
		 }
	  
		 /*====GameModelContent==============================================================================*/
		 public function addModel(model:GameModel):void {
			   var key:String =  getQualifiedClassName(model).split( "::" ).join( "." );
			   //if(_keyContent.add(key,GameModel)){
			    if (_keyContent[key] == null) {
					_keyContent[key] = GameModel;
			      _modelContent.add(model);
			   }
			}

		 public function containModel(classORpackname:*):Boolean {
			   var key:String;
			   if (classORpackname is Class) { key =  getQualifiedClassName(classORpackname).split( "::" ).join( "." ); }
			   key = classORpackname;
			   return   _modelContent.contain(key);
			}
			
		 public function getModel(classORpackname:*):GameModel{
			   var key:String;
			     if (classORpackname is Class) { 
				 key = getQualifiedClassName(classORpackname).split( "::" ).join( "." ); }else{
			     key = classORpackname;
			   }
			   return   _modelContent.get(key);
			}
	
	 
			
		 /*====GameControllContent==============================================================================*/
		 public function addController(controller:GameController):void {
			   var key:String =  getQualifiedClassName(controller).split( "::" ).join( "." );
			    if (_keyContent[key] == null){
				  _keyContent[key] = GameController;
			      _controllerContent.add(controller);
			   }
			}

		 public function containController(classORpackname:*):Boolean {
			   var key:String;
			   if (classORpackname is Class) { key =  getQualifiedClassName(classORpackname).split( "::" ).join( "." ); }
			   key = classORpackname;
			   return   _controllerContent.contain(key);
			}
			
		 public function getController(classORpackname:*):GameController{
			   var key:String;
			     if (classORpackname is Class) { 
				 key = getQualifiedClassName(classORpackname).split( "::" ).join( "." ); }else{
			     key = classORpackname;
			   }
			   return   _controllerContent.get(key);
			}
			
		 /*====property==============================================================================*/
		public function get count():int {
			 var len:int = 0;
			 for (var child:* in _keyContent) {
				    len++;
				 }
				return len;
			}
			
		public function getClass(className:String):Class {
			 return  Game.currentApplicationDomain.getDefinition(className) as Class;
			}
			
		public function getMovieClipByClass(className:String):MovieClip {
			    var cls:Class = getClass(className);
				return MovieClip(new cls())
			}
		 
		public function getSimpleButtonByClass(className:String):SimpleButton {
			    var cls:Class = getClass(className);
				return SimpleButton(new cls())
			}	
			
		public function getBitmapDataByClass(className:String):BitmapData {
			    var cls:Class = getClass(className);
				return BitmapData(new cls())
			}
		 
		public function getFont(className:String):Font {
			   var FontClass:Class = getClass(className) as Class;
			   Font.registerFont(FontClass);
			   return Font(new FontClass());
			}
			
		 public function containGameContent(key:String):Boolean {
				return  _keyContent[key]!=null;
			}
			
		public function  removeContent(key:String):void {
			    if (_keyContent[key] != null) {
					 switch(_keyContent[key]) {
					    case SwfContent:
							_swfContent.remove(key);
						break; 
						case ImageContent:
							_imageContent.remove(key);
							break;  
						case XMLLoaderContent:
							_xmloaderContent.remove(key);
							break;  
						case DataLoaderContent:
							_dataLoaderContent.remove(key);
							break;
						case XMLDataContent:
							_xmldataContent.remove(key);
							break;
						case BitmapDataGroupContent:
							_bitmapdatagroupContent.remove(key);
							break;
						 case ComponentContent:
							 _componentContent.remove(key);
							break; 
						case GameModelContent:
							 _modelContent.remove(key);
							break; 
						case GameControllerContent:
							_controllerContent.remove(key);
							break; 
						case ObjectContent:
							_objectContent.remove(key);
							break;
						 }
					}
			    delete _keyContent[key];
			   //_keyContent.removeByKey(key);
			}
        /**
         * 清空全部内容
         */
		public function clear():void {
				_keyContent = new Dictionary();
			/*for (var i:int = 0; i <_keyContent.data.length ; i++) 
			{
				removeContent(String(_keyContent.data[i].key));
			}*/
		}
	}
}